package com.zealcomm.ivcscustomer.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.AlertDialog;

import com.zealcomm.ivcscustomer.R;
import com.zealcomm.ivcscustomer.app.IvcsApplication;

public class AlertDialogUtil {

    private static AlertDialog alertDialog;

    public static AlertDialog show(View view) {
        dismiss();
        return show(view , 0);
    }

    @SuppressLint("ResourceType")
    public static AlertDialog show(View view , @StyleRes int theme) {
        dismiss();
        if (theme > 0){
            alertDialog = new AlertDialog.Builder(view.getContext() , theme)
                    .setView(view).create();
        }else {
            alertDialog = new AlertDialog.Builder(view.getContext())
                    .setView(view).create();
        }

        alertDialog.show();
        return alertDialog;
    }

    public static AlertDialog show(View view, View.OnClickListener onClickListener) {
        dismiss();
        View rootView = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_base_view, null);
        FrameLayout flParent = rootView.findViewById(R.id.fl_parent);
        flParent.addView(view);
        TextView tvCancel = rootView.findViewById(R.id.tv_cancel);
        TextView tvSure = rootView.findViewById(R.id.tv_sure);
        alertDialog = new AlertDialog.Builder(view.getContext())
                .setView(rootView).create();
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                onClickListener.onClick(v);
            }
        });
        alertDialog.show();
        return alertDialog;
    }

    public static AlertDialog show(Activity activity, @LayoutRes int layoutId, DialogInterface.OnClickListener onClickListener) {
        return show(activity, layoutId, null, null, onClickListener);
    }

    public static AlertDialog show(Activity activity, @LayoutRes int layoutId, String negativeText, String positiveText, DialogInterface.OnClickListener onClickListener) {
        dismiss();
        View rootView = LayoutInflater.from(activity).inflate(R.layout.dialog_base_view, null);
        FrameLayout flParent = rootView.findViewById(R.id.fl_parent);
        View view = LayoutInflater.from(activity).inflate(layoutId, flParent, true);
        // flParent.addView(view);
        TextView tvCancel = rootView.findViewById(R.id.tv_cancel);
        if (!TextUtils.isEmpty(negativeText)) {
            tvCancel.setText(negativeText);
        }
        TextView tvSure = rootView.findViewById(R.id.tv_sure);
        if ((!TextUtils.isEmpty(positiveText))) {
            tvSure.setText(positiveText);
        }
        alertDialog = new AlertDialog.Builder(activity)
                .setView(rootView).create();
        tvCancel.setOnClickListener(v -> onClickListener.onClick(alertDialog, AlertDialog.BUTTON_NEGATIVE));
        tvSure.setOnClickListener(v -> onClickListener.onClick(alertDialog, AlertDialog.BUTTON_POSITIVE));
        alertDialog.show();
        return alertDialog;
    }

    public static AlertDialog showSimpleDialog(Activity activity, String contentText, DialogInterface.OnClickListener onClickListener) {
        return showSimpleDialog(activity, contentText, null, null, onClickListener);
    }

    public static AlertDialog showSimpleDialog(Activity activity, String contentText, String negativeText, String positiveText, DialogInterface.OnClickListener onClickListener) {
        dismiss();
        View rootView = LayoutInflater.from(activity).inflate(R.layout.dialog_base_view, null);
        FrameLayout flParent = rootView.findViewById(R.id.fl_parent);
        TextView tvContent = (TextView) LayoutInflater.from(activity).inflate(R.layout.dialog_simple_content, flParent, false);
        tvContent.setText(contentText);
        flParent.addView(tvContent);
        TextView tvCancel = rootView.findViewById(R.id.tv_cancel);
        if (!TextUtils.isEmpty(negativeText)) {
            tvCancel.setText(negativeText);
        }
        TextView tvSure = rootView.findViewById(R.id.tv_sure);
        if ((!TextUtils.isEmpty(positiveText))) {
            tvSure.setText(positiveText);
        }
        alertDialog = new AlertDialog.Builder(activity)
                .setView(rootView).setCancelable(false).create();
        tvCancel.setOnClickListener(v -> onClickListener.onClick(alertDialog, AlertDialog.BUTTON_NEGATIVE));
        tvSure.setOnClickListener(v -> onClickListener.onClick(alertDialog, AlertDialog.BUTTON_POSITIVE));
        alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                dismiss();
            }
        });
        alertDialog.show();
        return alertDialog;
    }

    public static AlertDialog showSimpleDialog(String contentText, String negativeText, String positiveText, DialogInterface.OnClickListener onClickListener) {
        return showSimpleDialog(IvcsApplication.nowActivity, contentText, negativeText, positiveText, onClickListener);
    }

    public static void dismiss() {
        if (alertDialog != null) {
            if (alertDialog.isShowing()){
                alertDialog.dismiss();
            }
            alertDialog = null;
        }
    }
}
